package com.pangu.controller;

import com.pangu.model.User;
import com.pangu.service.intf.UserService;
import com.pangu.vo.PageVo;
import com.pangu.vo.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * *  Author:陈国强
 * *  Description:
 * *  Date:2022/12/29 18:48
 */
@RestController
@RequestMapping("/api/user/")
public class UserController {
    @Resource
    private UserService service;

    @PostMapping("add.do")
    public R add(User user){
        return service.add(user);
    }
    @PostMapping("update.do")
    public R update(User user){
        return service.update(user);
    }
    @PostMapping("delete.do")
    public R delete(int id, HttpSession session){
        return service.delete(id);
    }
    @GetMapping("page.do")
    public PageVo page(String username, String phone, int page, int limit){
        System.out.println(service.queryPage(username, phone, page, limit));

        return service.queryPage(username, phone, page, limit);
    }
    @PostMapping("login.do")
    public R login(String username,String password,String code,HttpSession session){
        return service.login(username, password, code, session);
    }

    @PostMapping("deletebatch.do")
    public R deleteBatch(int[] ids) {
        return service.deleteBatch(ids);
    }

    /**
     * 获取用户详情*/
    @GetMapping("detail.do")
    public R detail(HttpSession session){
        return R.ok(session.getAttribute("user"));
    }

    /**
     * 注销账号*/
    @GetMapping("loginout.do")
    public R loginout(HttpSession session){
        //注销session
        session.invalidate();
        return R.ok();
    }

    /**
     * 修改密码*/
    @PostMapping("updatepsw.do")
    public R updatePsw(String oldpassword,String newpassword,HttpSession session){
        return service.updatePsw(oldpassword, newpassword, session);
    }
}
